#include "host_data.h"

#define FRAM_DATA_INIT_KEY      0x0830

Host_Data_t host_data = RT_NULL;
struct rt_fram_device *fram_dev = RT_NULL;

static Host_Data_t host_data_create(void)
{
    Host_Data_t _host_data = rt_malloc(sizeof(struct Host_Data));

    return _host_data;
}
static rt_bool_t is_fram_data_inited(void)
{
    rt_uint16_t init_key;
    rt_device_read((rt_device_t)fram_dev, 500, &init_key, sizeof(init_key));
    return (init_key == FRAM_DATA_INIT_KEY);
}
void host_data_factory(void)
{
    rt_uint16_t init_key;

    /* 参数恢复出厂设置 */
    host_data->sv = 250;
    host_data->workMode = Work_Mode_Off;
    host_data->atEnable = 0;
    host_data->tecCtrlMode = TEC_Ctrl_Mode_Auto;
    host_data->tecPower = 0;
    host_data->pidKp = 200;
    host_data->pidKi = 30;
    host_data->pidKd = 500;
    host_data->pidMode = PID_Mode_A;
    host_data->tempAlarmLow = -200;
    host_data->tempAlarmHigh = 2000;
    host_data->svOffset = 0;
    host_data->pvOffset = 0;
    host_data->tecOutputDir = TEC_Output_Dir_Positive;
    host_data->tempSensorType = Temp_Sensor_Type_NTC;
    host_data->tempUnit = Temp_Unit_Type_Celsius;
    host_data->decimalPointNbr = Decimal_Point_Nbr_1;
    host_data->displayRangeLow = -1999;
    host_data->displayRangeHigh = 9999;
    host_data->filterDegree = 20;
    host_data->mbSlaverAddr = 1;
    host_data->mbBaudrateID = MB_Baudrate_ID_38400;
    host_data->powerOnAutoRun = 0;
    host_data->steadyAlarmRange = 0;
    host_data->tecMaxPower = 1000;
    host_data->mbCrcEnable = 1;
    host_data->errorCode = 0;
    host_data->progStart = 0;
    host_data->progCurrentStep = 1;
    host_data->progTimeUnitAdjust = Prog_Time_Unit_Sec;
    host_data->progTimeUnitHold = Prog_Time_Unit_Sec;
    host_data->progTempAcceptance = 100;
    for(uint8_t i = 0; i < 8; i++)
    {
        host_data->progList[i].stepNbr = i;
        host_data->progList[i].stepMode = Prog_Step_Mode_End;
        host_data->progList[i].stepSv = 250;
        host_data->progList[i].stepAdjustTime = 3600;
        host_data->progList[i].stepHoldTime = 3600;
        host_data->progList[i].stepLoopCnt = 1;
        host_data->progList[i].stepNext = &host_data->progList[(i + 1) % 8];
    }

    rt_device_read((rt_device_t)fram_dev, 500, (rt_uint8_t*)&init_key, sizeof(init_key));
    if(init_key != FRAM_DATA_INIT_KEY)
    {
        host_data->tempCalibPointNbr = 0;
        for(rt_uint8_t i = 0; i < 10; i++)
        {
            host_data->tempCalibReal[i] = 500 + 1000 * i;
            host_data->tempCalibSet[i] = 500 + 1000 * i;
        }
        rt_device_write((rt_device_t)fram_dev, get_member_offset(struct Host_Data, tempCalibReal[0]), (rt_uint8_t*)host_data, get_member_offset(struct Host_Data, tempCalibPointNbr));
        init_key = FRAM_DATA_INIT_KEY;
    }
    
    rt_device_write((rt_device_t)fram_dev, 0, (rt_uint8_t*)host_data, get_member_offset(struct Host_Data, tempCalibReal[0]));
    rt_device_write((rt_device_t)fram_dev, 500, (rt_uint8_t*)&init_key, sizeof(init_key));
    rt_kprintf("host data factory done\n\r");
}

void host_data_init(void)
{
    fram_dev = (struct rt_fram_device *)rt_device_find("fram");
    RT_ASSERT(fram_dev != RT_NULL);
    rt_device_open((rt_device_t)fram_dev, RT_DEVICE_OFLAG_RDWR);
    host_data = host_data_create();
    RT_ASSERT(host_data != RT_NULL);    

    if(is_fram_data_inited())
    {
        rt_device_read((rt_device_t)fram_dev, 0, host_data, sizeof(struct Host_Data));
        rt_kprintf("host data init done\n\r");
    }
    else
    {
        host_data_factory();
    }
}

